package com.demo.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo.model.JsonModel;
import com.demo.model.User;
import com.demo.service.UserService;
import com.demo.util.EscapeUnescape;
 
@Controller
@RequestMapping("/user")
public class UserController {
 
	@Autowired
	private UserService userService;
	
	 @RequestMapping(value="/userList.do")
	 public String selectAll(Model model){
	    	List<User> userList = userService.selectAll();
	    	List<User> userListFenye = new ArrayList<User>();
	    	int count = userList.size();
	    	int n = (count>=5) ? 5:count;
	    	
	    	for(int i = 0;i < n;i++){
	    		userListFenye.add(userList.get(i));
	    	}
	    	int zongye = 0;
	    	if(count%5 == 0){
	    		zongye = count/5;
	    	}else{
	    		zongye = count/5+1;
	    	}
	    	int dangqian = 1;
	    	
	    	model.addAttribute("userList", userListFenye);
	    	model.addAttribute("count", count);
	    	model.addAttribute("zongye", zongye);
	    	model.addAttribute("dangqian", dangqian);

			return "user/userList";
	  }
	 @RequestMapping(value="/selectUserFY.do")
	 public String selectUserFY(Integer dangqian,Integer sx,Model model){
	    	List<User> userList = userService.selectAll();
	    	List<User> userListFenye = new ArrayList<User>();
	    	
	    	int count = userList.size();
	    	int zongye = 0;
	    	if(count%5 == 0){
	    		zongye = count/5;
	    	}else{
	    		zongye = count/5+1;
	    	}
	    	if(sx == 1){ 			//上一页
    			dangqian = dangqian - 1;
    		}else if(sx == 2){       //下一页
    			dangqian = dangqian + 1;
    			if(dangqian == zongye);
    			dangqian = -1;
    		}
	    	
	    	int n = (count>=5) ? 5:count;
	    	
	    	if(dangqian == 0){
	    		for(int i = 0;i < n;i++){
		    		userListFenye.add(userList.get(i));
		    	}
	    		
		    	dangqian = 1;
	    	}else if(dangqian == -1){
	    		if(count%5 == 0){
		    		zongye = count/5;
		    	}else{
		    		zongye = count/5+1;
		    	}
	    		for(int i = (zongye-1)*5;i < count-1;i++){
		    		userListFenye.add(userList.get(i));
		    	}
		    	dangqian = zongye;
	    	}else{
	    		for(int i = (dangqian - 1)*5;i < dangqian*5;i++){
		    		userListFenye.add(userList.get(i));
		    	}
	    	}
	    	
	    	model.addAttribute("userList", userListFenye);
	    	model.addAttribute("count", count);
	    	model.addAttribute("zongye", zongye);
	    	model.addAttribute("dangqian", dangqian);

			return "user/userList";
	  }
	 @RequestMapping(value="/selectLike.do")
	 public String selectLike(User user,Model model){
	    	List<User> userList = userService.selectLike(user.getUseraccount(),user.getUsername(),user.getUsermobile());
	    	List<User> userListFenye = new ArrayList<User>();
	    	int count = userList.size();
	    	int n = (count>=5) ? 5:count;
	    	
	    	for(int i = 0;i < n;i++){
	    		userListFenye.add(userList.get(i));
	    	}
	    	int zongye = 0;
	    	if(count%5 == 0){
	    		zongye = count/5;
	    	}else{
	    		zongye = count/5+1;
	    	}
	    	int dangqian = 1;
	    	
	    	model.addAttribute("userList", userListFenye);
	    	model.addAttribute("count", count);
	    	model.addAttribute("zongye", zongye);
	    	model.addAttribute("dangqian", dangqian);
			return "user/userList";
	  }
	 @RequestMapping(value="/toAddUser.do")
	 public String toAddUser(){
			return "user/addUser";
	  }
	 
	 @RequestMapping(value="/addUser.do")
	 public String addUser(User user,Model model){
	
		    int n = userService.insert(user);
		    
	    	List<User> userList = userService.selectAll();
	    	model.addAttribute("userList", userList);
			return "redirect:userList.do";
	  }
	 
	 @RequestMapping(value="/toUpdateUser.do")
	 public String toUpdateUser(Integer id,Model model){
		 	User user = userService.selectById(id);
		 	model.addAttribute("user", user);
			return "user/updateUser";
	  }
	 @RequestMapping(value="/updateUser.do")
	 public String updateUser(User user,Model model){
	
		    int n = userService.updateByPrimaryKey(user);
		    
	    	List<User> userList = userService.selectAll();
	    	model.addAttribute("userList", userList);
			return "redirect:userList.do";
	  }
	 
	 @RequestMapping(value="/deleteUser.do")
	 public String deleteUser(Integer id,Model model){
		 	int n = userService.deleteByPrimaryKey(id);

		 	List<User> userList = userService.selectAll();
	    	model.addAttribute("userList", userList);
			return "redirect:userList.do";
	  }
	 
	 @RequestMapping(value="/index.do")
	    public String login(){
			return "login";
		}
		
	    @RequestMapping(value="/login.do")
	    public String select(User user,Model model){
	    	user = userService.selectByPrimaryKey(user.getUseraccount(), user.getUserpassword());
	    	if(user != null){
	    		model.addAttribute(user);
	    		return "backgroundsystem";
	    	}
			return "login";
	    }
	    
	    @InitBinder
	    protected void initBinder(WebDataBinder binder) {
	        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
	    }
	  
}